<template>
  <div class="com-container">
    <div class="com-chart" ref="rank_ref"></div>
  </div>
</template>

<script>
import { getRankData } from '@/API/api.js'
export default {
  name: 'Rank',
  data() {
    return {
      myChart: null,
      allData: null,
      startValue: 0,
      endValue: 9,
      timer: null
    }
  },
  mounted() {
    this.initChart()
    this.getData()
    window.addEventListener('resize', this.screenAdater)
    this.screenAdater()
  },
  destroyed() {
    window.removeEventListener('resize', this.screenAdater)
    clearInterval(this.timer)
  },
  methods: {
    // 初始化图表
    initChart() {
      // 初始化 echarts 对象
      this.myChart = this.$echarts.init(this.$refs.rank_ref, 'chalk')
      // 创建初始化配置对象
      const initOption = {
        title: {
          text: '▎地区销售排行',
          top: 20,
          left: 20,
          textStyle: {
            fontSize: 55
          }
        },
        grid: {
          top: '30%',
          left: '5%',
          right: '5%',
          bottom: '5%',
          containLabel: true
        },
        tooltip: {
          show: true
        },
        xAxis: {
          type: 'category'
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            type: 'bar',
            itemStyle: {
              borderRadius: [33, 33, 0, 0],
              barwidth: 66,
              color: (arg) => {
                const colorArr = [
                  ['#0BA82C', '#4FF778'],
                  ['#2E72BF', '#23E5E5'],
                  ['#5052EE', '#AB6EE5']
                ]
                let targetColorArr = null
                if (arg.value > 300) {
                  targetColorArr = colorArr[0]
                } else if (arg.value > 200) {
                  targetColorArr = colorArr[1]
                } else {
                  targetColorArr = colorArr[2]
                }
                return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: targetColorArr[0]
                  },
                  {
                    offset: 1,
                    color: targetColorArr[1]
                  }
                ])
              }
            }
          }
        ]
      }
      // 给 echarts 初始化对象添加 配置对象
      this.myChart.setOption(initOption)
      this.myChart.on('mouseover', () => {
        clearInterval(this.timer)
      })
      this.myChart.on('mouseout', () => {
        this.startInterval()
      })
    },
    // 获取数据
    async getData() {
      const { data: res } = await getRankData()
      // console.log(res)
      this.allData = res
      this.allData.sort((a, b) => {
        return b.value - a.value
      })
      this.updateChart()
      this.startInterval()
    },
    // 处理数据并更新图表
    updateChart() {
      const xAxisData = this.allData.map((item) => {
        return item.name
      })
      const yAxisData = this.allData.map((item) => {
        return item.value
      })
      const dataOption = {
        dataZoom: {
          show: false,
          startValue: this.startValue,
          endValue: this.endValue
        },
        xAxis: {
          data: xAxisData
        },
        series: [
          {
            data: yAxisData
          }
        ]
      }
      this.myChart.setOption(dataOption)
    },
    // 分辨率适配
    screenAdater() {
      const titleFontSize = (this.$refs.rank_ref.offsetWidth / 100) * 3.6
      const adaterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0]
            }
          }
        ]
      }
      this.myChart.setOption(adaterOption)
      this.myChart.resize()
    },
    startInterval() {
      if (this.timer) {
        clearInterval(this.timer)
      }
      this.timer = setInterval(() => {
        this.startValue++
        this.endValue++
        this.updateChart()
        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0
          this.endValue = 9
        }
      }, 2000)
    }
  }
}
</script>

<style lang="scss" scoped></style>
